﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;

namespace Crawler.Model
{
    public  class Crawler
    {

        string urls = "";
        public  string getdata(HtmlNode HNode, string tag, string className, string attr, int ElementAt)
        {
            try
            {
                HtmlNode H2Node = null;
                if (className != "")
                {
                    H2Node = HNode.Descendants(tag).Where(n => n.Attributes.Contains("class") && n.Attributes["class"].Value == className).ElementAtOrDefault(ElementAt);
                }
                else
                {
                    H2Node = HNode.Descendants(tag).ElementAtOrDefault(ElementAt);
                }
                if (H2Node != null)
                {
                    if (attr != "")
                    {
                        return H2Node.Attributes[attr].Value;
                    }
                    else
                    {
                        return H2Node.InnerText;
                    }
                }
            }
            catch (Exception )
            { 
            }
            return "";
        }

        public string GetInnerHtml(string strHtml, string id, bool IsInnerHtml)
        {
            id = replacestr(id);
            string result = "";
            HtmlDocument htmlDoc = new HtmlDocument();

            // There are various options, set as needed
            htmlDoc.OptionFixNestedTags = true;
            htmlDoc.LoadHtml(strHtml);
            // ParseErrors is an ArrayList containing any errors from the Load statement

            if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count() > 0)
            {
                result = "Handle any parse errors as required";
            }
            else
            {

                if (htmlDoc.DocumentNode != null)
                {
                    HtmlNode bodyNode = htmlDoc.DocumentNode.SelectSingleNode("//*" + id);//"//*[@id='" + id + "']"

                    if (bodyNode != null)
                    {
                        if (IsInnerHtml)
                        {
                            result = bodyNode.InnerHtml;
                        }
                        else {
                            result = bodyNode.OuterHtml;
                        }
                    }
                }
            }
            return result;
        }

        public  void RemoveTags(HtmlDocument html, string tagName)
        {
            try
            {
                var tags = html.DocumentNode.SelectNodes("//" + tagName);
                if (tags != null)
                {
                    foreach (var tag in tags)
                    {
                        tag.Remove();
                        if (!tag.HasChildNodes)
                        {
                            tag.ParentNode.RemoveChild(tag);
                            continue;
                        }

                        //for (var i = tag.ChildNodes.Count - 1; i >= 0; i--)
                        //{
                        //    var child = tag.ChildNodes[i];
                        //    tag.ParentNode.InsertAfter(child, tag);
                        //}
                        //tag.ParentNode.RemoveChild(tag);
                    }
                }
            }
            catch (Exception ex) { 
            
            }
        }

        public  HtmlNode GetHtmlNode(string strHtml, string id)
        {
            try
            {
                id = replacestr(id);
                HtmlDocument htmlDoc = new HtmlDocument();

                // There are various options, set as needed
                htmlDoc.OptionFixNestedTags = true;
                htmlDoc.LoadHtml(strHtml);
                // ParseErrors is an ArrayList containing any errors from the Load statement
                foreach (HtmlParseError err in htmlDoc.ParseErrors)
                {

                    Console.WriteLine("Error"+urls);
                    Console.WriteLine(" code=" + err.Code);
                    Console.WriteLine(" reason=" + err.Reason);
                    Console.WriteLine(" text=" + err.SourceText);
                    Console.WriteLine(" line=" + err.Line);
                    Console.WriteLine(" pos=" + err.StreamPosition);
                    Console.WriteLine(" col=" + err.LinePosition);
                    break;
                }

                //if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count() > 0)
                //{
                //    return null;
                //}
                //else
                //{
                if (htmlDoc.DocumentNode != null)
                {
                    return htmlDoc.DocumentNode.SelectSingleNode("//*" + id);
                }
                //}            
            }
            catch (Exception) {
                return null;
            }           
            return null;
        }
       
        public HtmlDocument GethtmlDoc(string strHtml)
        { 
            HtmlDocument htmlDoc = new HtmlDocument(); 
            htmlDoc.OptionFixNestedTags = true;
            htmlDoc.LoadHtml(strHtml);  
            if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count() > 0)
            {
                return null;
            }
            else
            {
                return htmlDoc;
            } 
        }
        public  string replacestr(string str)
        {
            string[] resul = null;
            resul = str.Split(new char[] { ' ' });
            string strresul = "";
            foreach (string item in resul)
            {
                if (item.Contains("#"))
                {
                    strresul += "[@id='" + item.Replace("#", "") + "']";
                }
                else if (item.Contains("."))
                {
                    strresul += "[@class='" + item.Replace(".", "") + "']";
                }
                else if (item.Contains("&")) {
                    strresul += "[@class='" + item.Replace("&", "") + "']"; 
                }
                else
                {
                    strresul += "/" + item;
                }



            }
            return strresul;
        }

        //public  string ExtractContent(string url, string Dieukien, int Nhom)
        //{
        //    string content = DownloadHtmlString(url);

        //    if (content != null)
        //    {
        //        Regex regEx = new Regex(Dieukien, RegexOptions.IgnoreCase);
        //        Match match = regEx.Match(content);
        //        if (match != null && match.Success)
        //        {
        //            return match.Value;
        //        }
        //    }
        //    return "";
        //}
        


        
    }
    public class CrawlerClient {
       private static CookieAwareWebClient client = new CookieAwareWebClient(); 
        public CrawlerClient(){
            client = cookieAwareWebClient();
        }
        public string DownloadHtmlStrings(string url)
        { 
            string Reval = string.Empty;  
            WebClient client = new WebClient();
            client.Headers.Add("user-agent", "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5");

            client.Headers.Add("Accept-Encoding", "");
            try
            {  
                Reval = client.DownloadString(url);
                
            }
            catch (Exception ex)
            {
                Reval = "erro:" + ex.Message;
                
                DownloadHtmlString(url);
            }
            return Reval;
        }   
        public string DownloadHtmlString(string url)
        {
           // var client = cookieAwareWebClient();
            return client.DownloadString(url);
        }
        public CookieAwareWebClient cookieAwareWebClient()
        {
           // var client = new CookieAwareWebClient();
            client.Encoding = System.Text.Encoding.UTF8;
            client.BaseAddress = @"http://domain.net/";
            var loginData = new NameValueCollection();
            loginData.Add("login", "user");
            loginData.Add("password", "pass");
            client.UploadValues("login/login", "POST", loginData);
            return client;
        }
    }
    public class CookieAwareWebClient : WebClient
    {
        private CookieContainer cookie = new CookieContainer();

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            request.ContentType = "application/x-www-form-urlencoded";
            if (request is HttpWebRequest)
            {
                (request as HttpWebRequest).CookieContainer = cookie;
            }
            return request;
        }
    }
}
